package com.sisqorap.puzzelalarm;

import com.sisqorap.puzzelalarm.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class SnoozeAlarm extends Activity{
	private Toast mToast;
	private MediaPlayer MPX;
	private int snoozeTime;
	private Alarm alarm;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
	    setContentView(R.layout.snooze);
	    alarm = new Alarm(SnoozeAlarm.this);
	    Settings setting = new Settings(this);
	    snoozeTime = setting.getSnoozeTime();
	    
	    // Watch for button clicks.
	    Button button = (Button)findViewById(R.id.snooze_alarm5);
	    button.setOnClickListener(mStartSnooze5);
	    button.setText((button.getText() + "").replace("[SNOOZETIME]", "+" + snoozeTime));
	    button = (Button)findViewById(R.id.btnEndAlarm);
	    button.setOnClickListener(mEndAlarm);
	    
	    Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        MPX = new MediaPlayer();
        //Log.i("MediaPlayer", "Created");
        try{
	        MPX.setDataSource(this, alert);
	        //Log.i("MediaPlayer", "Data Source set");
	        final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
	        //Log.i("AudioManager", "Audio Manager OK");
	        if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
                MPX.setAudioStreamType(AudioManager.STREAM_ALARM);
                MPX.setLooping(true);
                MPX.prepare();
                MPX.start();
	        }
        }catch(Exception ex){
        	Log.i("Exception", "Exception" );
        }
	}

	private OnClickListener mStartSnooze5 = new OnClickListener() {
        public void onClick(View v) {
        	if(MPX.isPlaying()){
        		MPX.stop();
        	}
        	String sb = alarm.SnoozeFirstAlarm();
            mToast = Toast.makeText(SnoozeAlarm.this, sb, Toast.LENGTH_LONG);
            mToast.show();
            SnoozeAlarm.this.finish();
        }
    };
    
    private OnClickListener mEndAlarm = new OnClickListener() {
        public void onClick(View v) {
        	if(MPX.isPlaying()){
        		MPX.stop();
        	}
        	alarm.endFirstAlarm();
        	Intent i = new Intent(SnoozeAlarm.this, EndAlarm.class);
        	i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        	startActivity(i);
        	SnoozeAlarm.this.finish();
        }
    };
}